<?php

namespace App\Admin\Metrics\Examples;

use App\Models\BoxAssignment;
// use App\Admin\Repositories\BoxAssignment;
use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;

class Tickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();
        $this->title('打卡记录');
        $this->height(400);
        $this->chartHeight(400);
        $this->chartLabels('已完成打卡学员');
        $this->dropdown([
            '7' => '七天内',
            '28' => '28天内',
            '30' => '一个月以内',
            '365' => '一年以内',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '365':
            case '30':
            case '28':
            case '7':
                // 连接到 box_assignment 表并统计不同状态的用户数量
                $completedCount = BoxAssignment::where('status', 1)->count();
                // return $completedCount;
                $incompleteCount = BoxAssignment::where('status', 2)->count();
                $notCompletedCount = BoxAssignment::where('status', 0)->count();

                // 调试输出获取的数据
                // var_dump($completedCount, $incompleteCount, $notCompletedCount);

                // 设置卡片内容
                $totalUsers = $completedCount + $incompleteCount + $notCompletedCount;
                // var_dump($totalUsers);
                $this->withContent($totalUsers);

                // 设置图表数据
                $this->withChart([$completedCount, $incompleteCount, $notCompletedCount]);

                break;

            // default:
            //     // 卡片内容
            //     $this->withContent(162);
            //     // 卡片底部
            //     $this->withFooter(29, 63, '1d');
            //     // 图表数据
            //     $this->withChart([83]);

            //     break;
        }
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <small>打卡已完成</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>新用户</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <!-- <div class="text-center">
        <p>Open Tickets</p>
        <span class="font-lg-1">{$open}</span>
    </div> -->
    <div class="text-center">
        <p>{$response}</p>
        <span class="font-lg-1"></span>
    </div>
</div>
HTML
        );
    }
}
